/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.app;

import han.bifang.business.dao.ApplicationDao;
import han.bifang.business.dao.ClientAgentDao;
import han.bifang.business.dao.ServiceAgentDao;
import han.bifang.business.dao.TreeDao;
import han.bifang.common.entity.SoaAppDetail;
import han.bifang.common.entity.SoaApplication;
import han.bifang.common.entity.SoaTreeUnit;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class AppManager {
	
	public static ArrayList<String> createApplication(SoaTreeUnit t, SoaApplication d)
			throws BifangException {
		
		try {
			
			ArrayList<String> ret = new ArrayList<String>();
			
			TreeDao.createTreeUnit(t);
			d.setUnitId(t.getId());
			ApplicationDao.createApp(d);
			t.setKey(d.getId());
			TreeDao.modifyTreeUnit(t);
			ret.add(t.getId());
			ret.add(d.getId());
			
			
			return ret;
		} catch (BifangException e) {
			
			throw e;
			

		}

	}

	public static void modifyApplication(SoaTreeUnit t, SoaApplication d)
			throws BifangException {
		
		try {
			
			TreeDao.modifyTreeUnit(t);
			ApplicationDao.modifyApp(d);
			
		} catch (BifangException e) {
			
			throw e;

		}

	}

	public static void modifyApplication(SoaApplication d) throws BifangException {
		
		try {
			
			ApplicationDao.modifyApp(d);
			SoaTreeUnit t = TreeDao.getTreeUnit(d.getUnitId());
			t.setName(d.getName());
			TreeDao.modifyTreeUnit(t);
			
		} catch (BifangException e) {
			
			throw e;

		}

	}

	

	public static void removeApplication(String appId)
			throws BifangException {
		
		try {
			
			SoaApplication sd = ApplicationDao.getApp(appId);
			if(sd == null)
				throw new BifangException(BifangErrorCode.RECORD_NOT_EXISTS_ERROR,"not exists");
			List c = ServiceAgentDao.getServiceAgentByAppId(appId);
			if (c.size() > 0) {
				
				throw new BifangException(
						BifangErrorCode.APP_HAS_SERVICE_AGENT,
						"application has service agents");

			}
			c = ClientAgentDao.getClientAgentByAppId(appId);
			if (c.size() > 0) {
				
				throw new BifangException(
						BifangErrorCode.APP_HAS_CLIENT_AGENT,
						"application has client agents");

			}

			

			TreeDao.removeTreeUnit(sd.getUnitId());
			ApplicationDao.removeApp(appId);
			
		} catch (BifangException e) {
			
			throw e;

		}
	}

	public static List getAllApps() throws BifangException {
		
		try {
			

			List l = ApplicationDao.getAllApps();
			
			return l;
		} catch (BifangException e) {
			
			throw e;

		}

	}

	public static SoaAppDetail getApplicationDetail(String id)
			throws BifangException {
		
		try {
			
			SoaAppDetail dd = new SoaAppDetail();
			dd.setApplication(ApplicationDao.getApp(id));
			dd.setServiceAgents(ServiceAgentDao.getServiceAgentByAppId(id));
			dd.setClientAgents(ClientAgentDao.getClientAgentByAppId(id));
			
			
			return dd;

		} catch (BifangException e) {
			
			throw e;

		}

	}

}
